package com.timoon.webcomponent.utility.titlelist.singlepassage;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.Options;
import com.timoon.app.resource.image.ImageLocation;
import com.timoon.component.behavior.bootstrap.AbstractBootStrapBehavior;
import com.timoon.sample.dao.PassageDAO;
import com.timoon.sample.dao.PassagePicDAO;
import com.timoon.sample.dao.model.Passage;
import com.timoon.sample.dao.model.PassagePic;
import com.timoon.sample.dao.model.PassagePicExample;
import com.timoon.webcomponent.BaseWebPanel;

/**
 *<p>单篇文章显示</p>
 *<p>public</p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class SinglePassageV1Panel extends BaseWebPanel {

	@SpringBean
	private PassageDAO passageDAOImpl;
	@SpringBean
	private PassagePicDAO passagePicDAOImpl;
	
	private Integer passage_id;
	
	public SinglePassageV1Panel(String id,PageParameters pageParameter) {
		super(id);
		// TODO Auto-generated constructor stub
		
		if(pageParameter.get("passage_id").isEmpty()){
			throw new RuntimeException("passage_id 参数不存在");
		}
		
		//引用bootstrap
		this.add(new AbstractBootStrapBehavior("#ddddddd", "click", new Options()));
		
		this.passage_id=pageParameter.get("passage_id").toInteger();
		
		this.doPanel();
	}
	
	private void doPanel(){
		
		Passage passage=this.loadPassageById(passage_id);
		
		add(new Label("wk_code",new PropertyModel(passage, "code")));
		add(new Label("wk_title",new PropertyModel(passage, "title")));
		add(new Label("wk_date",new PropertyModel(passage, "updateDate")));
		add(new Label("wk_author",new PropertyModel(passage, "author")));
		add(new Label("wk_read_count",new PropertyModel(passage, "readCount")));
		add(new Label("wk_content",new PropertyModel(passage, "content")));
		
		this.renderPic();
	}
	@SuppressWarnings("unchecked")
	private void renderPic(){
		
		List<PassagePic> ppList=this.passagePicDAOImpl.selectByExample(new PassagePicExample(){{
			this.createCriteria().andPassageIdEqualTo(passage_id);
		}});
		
		ListView<PassagePic> listView=new ListView<PassagePic>("wk_pic_list",ppList) {
			
			@Override
			protected void populateItem(ListItem<PassagePic> arg0) {
				// TODO Auto-generated method stub
				PassagePic pic=arg0.getModelObject();
				
				Image img=new Image("wk_pic",new  PackageResourceReference(ImageLocation.class, pic.getPath()));
				img.add(new AttributeModifier("title",pic.getTitle()));
				
				arg0.add(img);
			}
		};
		add(listView);
	}
	private Passage loadPassageById(Integer id){
		Passage passage=this.passageDAOImpl.selectByPrimaryKey(passage_id);
		Integer readCount=passage.getReadCount();
		passage.setReadCount(readCount+1);
		
		this.passageDAOImpl.updateByPrimaryKeyWithBLOBs(passage);
		return passage;
	}

	public PassageDAO getPassageDAOImpl() {
		return passageDAOImpl;
	}

	public void setPassageDAOImpl(PassageDAO passageDAOImpl) {
		this.passageDAOImpl = passageDAOImpl;
	}

	public PassagePicDAO getPassagePicDAOImpl() {
		return passagePicDAOImpl;
	}

	public void setPassagePicDAOImpl(PassagePicDAO passagePicDAOImpl) {
		this.passagePicDAOImpl = passagePicDAOImpl;
	}

}
